package cn.toutatis.sandbox.root.exception;


import cn.toutatis.common.config.ResultCode;
import cn.toutatis.common.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Toutatis_Gc
 */
@ControllerAdvice
public class ExceptionAdviceDispose {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @ResponseBody
    @ExceptionHandler({NullPointerException.class,ArithmeticException.class})
    public Result errorMsg(HttpServletRequest request, Exception e) throws ServletException, IOException {
        logger.error(request.getMethod(),e.getMessage());
        Result result = new Result(false);
        result.setCode(ResultCode.REQUEST_EXCEPTION);
        result.setMessage(ResultCode.REQUEST_EXCEPTION);
        return result;
    }
}
